package com.wingsboat.common.protect.lockProcessor.impl;

import com.wingsboat.common.protect.aspect.AthenaLockAspect;
import com.wingsboat.common.protect.lockProcessor.AthenaLockProcessor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author wangzhong
 * @description: 单机版锁处理器
 * @date 2024/5/30 16:01
 */
@Component
public class DefaultLockProcessor implements AthenaLockProcessor, InitializingBean {

    @Autowired
    private AthenaLockAspect athenaLockAspect;
    /**
     * 处理
     * @param joinPoint
     * @param key
     * @param leaseTime
     * @return
     * @throws Throwable
     */
    @Override
    public Object proceed(ProceedingJoinPoint joinPoint, String key, long leaseTime) throws Throwable {
        synchronized (key.intern()) {
            return joinPoint.proceed();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        athenaLockAspect.addAthenaLockProcessor(this);
    }
}
